﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Providers.Entities;
using Model;
using System.Web.Mvc;
using System.Web.Routing;
using SYT.AliApiHandle;

namespace SYT.Web.liberty
{
    public class MasterPage : Controller
    {
        public static Users user;

        /// <summary>
        /// 调用父类初始化方法
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            ////判断Session为空，调转到阿里登陆页
            //if (System.Web.HttpContext.Current.Session["UserreAllInfo"] == null || System.Web.HttpContext.Current.Session["UserreAllInfo"].ToString() == "")
            //{
            //    System.Web.HttpContext.Current.Response.Redirect("/Index/Eror", true);

            //    return;
            //    //System.Web.HttpContext.Current.Response.End();
            //}
            //else
            //{
            //    //读取Session中的用户对象。
            //    user = JsonConvert.DeserializeObject<User>(System.Web.HttpContext.Current.Session["UserreAllInfo"].ToString());
            //    //读取数据库中的用户对象，避免自动更新临时令牌时，令牌失效的情况。
            //    user = UserBLL.GetUser(user.aliId, user.memberId);
            //    ViewBag.username = user.resource_owner;
            //    base.Initialize(requestContext);
            //}


            user = JsonConvert.DeserializeObject<Users>(Session["UserreAllInfo"].ToString());
            ViewBag.username = user.resource_owner;

            //加载所有产品数量和状态。
            LoadResource();
        }
        /// <summary>
        /// 加载所有产品数量和状态。
        /// </summary>
        private void LoadResource()
        {
            RouteValueDictionary dic = this.ControllerContext.HttpContext.Request.RequestContext.RouteData.Values;
            if (dic["action"].ToString() == "Index1" && dic["controller"].ToString() == "Product")
            {
                //获取店铺所有产品所属自定义类目的集合和产品状态集合。
                ViewBag.SellerCats = GetApiShopInfo.GetSellerCats(user.memberId, user.access_token, null);
            }
            if (dic["action"].ToString() != "Index" && dic["controller"].ToString() == "ProductBatchEdit")
            {
                string Currentcount = "0";
                string parameter1 = Request["parameter1"];
                string parameter2 = Request["parameter2"];
                string PageIndex = Request["PageIndex"];
                //根据搜索条件请求数据
                ViewBag.ProductModels = GetApiShopInfo.GetProListEdit(PageIndex, parameter1, parameter2, user.memberId, out Currentcount);
            }
        }
    }
}